<?php

namespace Form\Ocean\Ranking;


use App\Dao\Exam\ExamDao;
use App\Dao\Ocean\OceanRankingSettingDao;
use App\Dao\Task\TaskHelpDao;
use Form\BaseForm;
use App\Model\Task\TaskHelpCategory;
use Yj\Apidog\Annotation\ApiController;
use Yj\Apidog\Annotation\FormData;
use Yj\Apidog\Annotation\PostApi;
use Yj\ColumnBuilder\Column;
use Yj\Form\Elm;

/**
 * @ApiController(prefix="form/ocean/ranking/setting")
 */
class Setting extends \Yj\Form\BaseForm
{

    protected $yjCode = 'ocean/ranking/setting';

    const ADD = true;
    const EDIT = true;
    const DELETE = true;


    public function getColumn()
    {
        $colum = Column::create();
        $colum->setRules([
            Column::txt()->setLabel('名次')->setProp('rank'),
            Column::txt()->setLabel('奖励小豆')->setProp('reward'),
        ]);
        return $colum->toArray();
    }

    /**
     * @PostApi(path="config",description="配置")
     */
    public function config()
    {
        return $this->_config();
    }

    /**
     * @PostApi(path="update",description="添加")
     * @FormData(key="id",rule="nullable")
     */
    public function update()
    {
        $id = $this->getParams()->get('id');

        $form = Elm::createForm($this->getRoute($id ? 'edit' : 'add'));

        $form->setRule([
            Elm::number('rank', '名次')->required(),
            Elm::number('reward', '奖励小豆')->required(),
        ]);
        $id && $form->append(
            Elm::hidden('id', $id)
        );
        $lists = $form->setTitle($id ? '编辑' : '添加' . '奖励小豆')->formData(function () use ($id) {
            $formData = [];
            if ($id) {
                $formData = App(OceanRankingSettingDao::class)->where('id', $id)->first();
            }
            return $formData;
        });
        _SUCCESS(formToData($lists));
    }


}